package com.supreme.consumerserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonelastic.entity.ESInputHistory;
import com.supreme.consumerserver.service.ElasticsearchService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "elasticsearch")
public class ElasticsearchController {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Resource
    private ElasticsearchService elasticsearchService;

    @PostMapping(value = "createIndex/{indexName}")
    public AjaxResp createIndex(@PathVariable(value = "indexName") String indexName) throws IOException {
        boolean serviceIndex = elasticsearchService.createIndex(indexName);
        return AjaxResp.instance(serviceIndex);
    }

    @GetMapping(value = "searchIndex")
    public AjaxResp indexExist() throws IOException {
        List<String> indexList = elasticsearchService.searchIndex();
        return AjaxResp.instance(indexList);
    }

    @GetMapping(value = "indexExist/{indexName}")
    public AjaxResp indexExist(@PathVariable(value = "indexName") String indexName) throws IOException {
        boolean existIndex = elasticsearchService.existIndex(indexName);
        return AjaxResp.instance(existIndex);
    }

    @DeleteMapping(value = "deleteIndex/{indexName}")
    public AjaxResp deleteIndex(@PathVariable(value = "indexName") String indexName) throws IOException {
        boolean deleteIndex = elasticsearchService.deleteIndex(indexName);
        return AjaxResp.instance(deleteIndex);
    }

    @PostMapping(value = "createDocument/{indexName}")
    public AjaxResp createDocument(@PathVariable(value = "indexName") String indexName, @RequestBody ESInputHistory bodyData) throws IOException {
        int result = elasticsearchService.createDocument(indexName, bodyData, null);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "searchDocument/{indexName}")
    public AjaxResp searchDocument(@PathVariable(value = "indexName") String indexName, Map<String, String> queryParams) throws IOException {
        Integer offset = Integer.parseInt(queryParams.getOrDefault("offset", "0"));
        Integer limit = Integer.parseInt(queryParams.getOrDefault("limit", "10"));
        List<JSONObject> dataList = elasticsearchService.searchDocument(indexName, JSONObject.class, offset, limit);
        return AjaxResp.instance(dataList);
    }

    @PutMapping(value = "updateDocument/{indexName}/{documentId}")
    public AjaxResp updateDocument(@PathVariable(value = "indexName") String indexName, @PathVariable(value = "documentId") String documentId, @RequestBody ESInputHistory bodyData) throws IOException {
        int result = elasticsearchService.updateDocument(indexName, documentId, bodyData);
        return AjaxResp.instance(result);
    }

    @DeleteMapping(value = "deleteDocument/{indexName}/{documentId}")
    public AjaxResp deleteDocument(@PathVariable(value = "indexName") String indexName, @PathVariable(value = "documentId") String documentId) throws IOException {
        int result = elasticsearchService.deleteDocument(indexName, documentId);
        return AjaxResp.instance(result);
    }
}
